{% extends "app/base_site.html" %}
{% block title %} {% if  "add" == handle %}添加{% else %}编辑{% endif %}基础算法 {% endblock title %}
{% block stylesheets %}
  {{ block.super }}
    <style>
    input[type="file"]{
      color: #8d2f2f;
      font-size: 12px;
    }
    ::file-selector-button{
      font-size: 12px;
      color: #fff;
      border-radius: 4px;
      border: 1px solid #2a80eb;
      padding: 2px 4px;
      background-color: #2a80eb;
      cursor: pointer;
    }

</style>
{% endblock stylesheets %}

{% block content %}

  <div class="right_col" role="main">
    <div class="">
      <div class="row">
        <div class="col-md-12 col-xs-12">
          <div class="x_panel">
            <div class="x_title">
                <h2>{% if  "add" == handle %}添加{% else %}编辑{% endif %}基础算法 <span id="top_msg">{{top_msg}}</span></h2>
              <!--
                <ul class="nav navbar-right panel_toolbox">
                <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                </li>
                <li class="dropdown">
                  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
                  <ul class="dropdown-menu" role="menu">
                    <li><a href="#">Settings 1</a>
                    </li>
                    <li><a href="#">Settings 2</a>
                    </li>
                  </ul>
                </li>
                <li><a class="close-link"><i class="fa fa-close"></i></a>
                </li>
              </ul>
            -->
              <div class="clearfix"></div>
            </div>
            <div class="x_content">
              <br>
              <form class="form-horizontal form-label-left"  method="post" enctype="multipart/form-data" data-parsley-validate  >

                     <input type="hidden" name="handle"  value="{{ handle }}">
                     <input type="hidden" name="code"  value="{{ algorithm.code }}">

                <div class="form-group">
                  <label class="control-label col-md-3 col-sm-3 col-xs-12">算法编号 </label>
                  <div class="col-md-3 col-sm-3 col-xs-12">
                     <input type="text"  value="{{ algorithm.code }}" class="form-control" disabled="disabled">
                  </div>
                </div>

            <div class="form-group">
                  <label class="control-label col-md-3 col-sm-3 col-xs-12">算法名称 <span class="required">*</span></label>
              <div class="col-md-3 col-sm-3 col-xs-12">
                    <input type="text" name="name" value="{{ algorithm.name }}" required="required"  class="form-control">
                  </div>
                </div>

                  <div class="form-group">
                  <label class="control-label col-md-3 col-sm-3 col-xs-12">布控上限 <span class="required">*</span> <i class="bxc_introduce fa fa-info-circle" title="布控上限是指该基础模型最大支持多少个布控同时使用"></i></label></label>
              <div class="col-md-3 col-sm-3 col-xs-12">
                    <input type="number" name="control_max_count" value="{{ algorithm.control_max_count }}" required="required"  class="form-control">
                  </div>
                </div>

{% if "edit" == handle %}<input type="hidden" name="way_code" value="{{ algorithm.way_code }}">{% endif %}

                  <div class="form-group">
                  <label class="control-label col-md-3 col-sm-3 col-xs-12">请选择接入方式 <span class="required">*</span></label>
             <div class="col-md-3 col-sm-3 col-xs-12">
                    <select name="way_code" {% if "edit" == handle %}disabled="disabled"{% endif %} class="form-control">

                        <option value="0">请选择</option>
                        {% for way in algorithm_ways %}
                            {% if algorithm.way_code == way.code %}
                                <option selected value="{{ algorithm.way_code }}">{{ way.name }}</option>
                            {% else %}
                               <option value="{{ way.code }}">{{ way.name }}</option>
                            {% endif %}
                        {% endfor %}
                    </select>
                  </div>
                </div>

             <div class="form-group way_code_API">
                  <label class="control-label col-md-3 col-sm-3 col-xs-12">接入参数 <span class="required">*</span>
                  </label>
                  <div class="col-md-9 col-sm-9 col-xs-12">
                    <textarea name="way_value" class="form-control" rows="2" placeholder="">{{ algorithm.way_value }}</textarea>
                  </div>
                </div>


               <div class="form-group way_code_MODEL">
                  <label class="control-label col-md-3 col-sm-3 col-xs-12">算法框架 <span class="required">*</span></label>
                 <div class="col-md-3 col-sm-3 col-xs-12">
                    <select name="framework" {% if "edit" == handle %}disabled="disabled"{% endif %} class="form-control">
                        <option value="0">请选择</option>
                        {% for framework in algorithm_frameworks %}
                            <option {% if algorithm.framework == framework %}selected{% endif %} value="{{ framework }}">{{ framework }}</option>
                        {% endfor %}
                    </select>
                  </div>
                </div>


               <div class="form-group way_code_MODEL">
                  <label class="control-label col-md-3 col-sm-3 col-xs-12">模型推理平台 <span class="required">*</span></label>
                 <div class="col-md-3 col-sm-3 col-xs-12">
                    <select name="inference" {% if "edit" == handle %}disabled="disabled"{% endif %} class="form-control">
                        <option value="0">请选择</option>
                        {% for inference in algorithm_inferences %}
                            <option {% if algorithm.inference == inference %}selected{% endif %} value="{{ inference }}">{{ inference }}</option>
                        {% endfor %}
                    </select>
                  </div>
                </div>

                             <div class="form-group way_code_MODEL">
                  <label class="control-label col-md-3 col-sm-3 col-xs-12">模型推理设备 <span class="required">*</span> <i class="bxc_introduce fa fa-info-circle" title="模型推理设备是运算模型的设备，比如CPU,GPU,GPU:0,GPU:1"></i></label>
              <div class="col-md-3 col-sm-3 col-xs-12">
                    <input type="text" name="inference_device" value="{{ algorithm.inference_device }}"  class="form-control">

              </div>
                </div>

             <div class="form-group way_code_MODEL">
                  <label class="control-label col-md-3 col-sm-3 col-xs-12">推理输入尺寸（宽）<span class="required">*</span></label>
              <div class="col-md-3 col-sm-3 col-xs-12">
                    <input type="number" name="infer_width" value="{{ algorithm.infer_width }}"  class="form-control">

              </div>
                </div>

             <div class="form-group way_code_MODEL">
                  <label class="control-label col-md-3 col-sm-3 col-xs-12">推理输入尺寸（高）<span class="required">*</span></label>
              <div class="col-md-3 col-sm-3 col-xs-12">
                    <input type="number" name="infer_height" value="{{ algorithm.infer_height }}"  class="form-control">
              </div>
                </div>

                <div class="form-group way_code_MODEL">
                    <label class="control-label col-md-3 col-sm-3 col-xs-12">模型文件 <span class="required">*</span></label>

                    <div class="col-md-3 col-sm-3 col-xs-12">
                      <input type="file" name="model" accept="application/x-tar"  {% if "edit" == handle %}disabled="disabled"{% endif %} class="form-control" placeholder="模型文件" >
                    </div>

                </div>
{% if "edit" == handle %}
                  <div class="form-group way_code_MODEL">
                      <label class="control-label col-md-3 col-sm-3 col-xs-12">模型文件夹路径</label>
                  <div class="col-md-9 col-sm-9 col-xs-12">
                     <input type="text" value="{{ absolute_model_dir }}" readonly  class="form-control">
                  </div>
                </div>

                  <div class="form-group way_code_MODEL">
                      <label class="control-label col-md-3 col-sm-3 col-xs-12">模型识别分类</label>
                  <div class="col-md-9 col-sm-9 col-xs-12">
                    <textarea disabled="disabled" class="form-control" rows="6" >{{ algorithm.model_class_names }}</textarea>
                  </div>
                </div>

                  <div class="form-group way_code_MODEL">
                  <label class="control-label col-md-3 col-sm-3 col-xs-12">模型备注</label>
                    <div class="col-md-3 col-sm-3 col-xs-12">
                    <input type="text" value="{{ algorithm.model_remark }}" disabled="disabled" class="form-control">
                  </div>
                </div>

                      <div class="form-group way_code_MODEL">
                  <label class="control-label col-md-3 col-sm-3 col-xs-12">模型文件夹</label>
                    <div class="col-md-3 col-sm-3 col-xs-12">
                    <input type="text" value="{{ algorithm.model_name_dir }}" disabled="disabled" class="form-control">
                  </div>
                </div>
            {% endif %}


                  <div class="form-group">
                  <label class="control-label col-md-3 col-sm-3 col-xs-12">备注
                  </label>
                  <div class="col-md-9 col-sm-9 col-xs-12">
                    <textarea name="remark" class="form-control" rows="3" placeholder="">{{ algorithm.remark }}</textarea>
                  </div>
                </div>

                <div class="form-group">
                  <div class="col-md-9 col-sm-9 col-xs-12 col-md-offset-3">
                    <button type="button" onclick="window.history.go(-1)" class="btn btn-primary">取消</button>
                    <button type="submit" class="btn btn-success">提交</button>
                  </div>
                </div>

              </form>
            </div>
          </div>
        </div>
      </div>

    </div>
  </div>
{% endblock content %}

{% block javascripts %}
  {{ block.super }}
<script>

let handle = "{{ handle }}";
let algorithm_code = "{{ algorithm.code }}";
let algorithm_way_code = "{{ algorithm.way_code }}";
let ele_input_name= $("input[name='name']");
let eleInputControlMaxCount = $("input[name='control_max_count']"); //control_max_count
let ele_select_way_code= $("select[name='way_code']");
let ele_input_inference_device= $("input[name='inference_device']");
let ele_textarea_way_value = $("textarea[name='way_value']");
let ele_select_framework= $("select[name='framework']");
let ele_select_inference= $("select[name='inference']");
let ele_input_model= $("input[name='model']");
let ele_textarea_remark= $("textarea[name='remark']");

let selectedWayCode = algorithm_way_code;


ele_select_way_code.change(function () {
    selectedWayCode = $(this).val();
    if("MODEL" === selectedWayCode){
        $(".way_code_MODEL").show();
        $(".way_code_API").hide();
    }else if("API" === selectedWayCode){
        $(".way_code_MODEL").hide();
        $(".way_code_API").show();
    }else{
        $(".way_code_MODEL").hide();
        $(".way_code_API").hide();
    }

});

$(document).ready(function () {
    if("edit" === handle){
        if("MODEL" === algorithm_way_code){
            $(".way_code_MODEL").show();
            $(".way_code_API").hide();
        }else if("API" === algorithm_way_code){
            $(".way_code_MODEL").hide();
            $(".way_code_API").show();
        }else{
            $(".way_code_MODEL").hide();
            $(".way_code_API").hide();
        }
    }else{
        $(".way_code_MODEL").hide();
        $(".way_code_API").hide();
    }

   $("form").submit(function () {
       console.log("selectedWayCode=",selectedWayCode);

       if(ele_input_name.val().trim() === ""){
            myAlert("请输入算法名称","error");
            return false;
        }

       try {
            let controlMaxCount = parseInt(eleInputControlMaxCount.val());
            if(isNaN(controlMaxCount)){
                myAlert("布控上限不能为空","error");
                return false;
            }
            if(controlMaxCount<1){
                myAlert("布控上限不能小于1","error");
                return false;
            }
       }catch (e) {
            myAlert("布控上限格式不正确","error");
            return false;
       }
      if("MODEL" === selectedWayCode){
        if(ele_select_framework.val().trim()==="0"){
            myAlert("请选择算法框架","error");
            return false;
        }

        if(ele_select_inference.val().trim()==="0"){
            myAlert("请选择模型推理平台","error");
            return false;
        }
        let inference_device = ele_input_inference_device.val().trim();
        if(inference_device === ""){
            myAlert("请输入模型推理设备","error");
            return false;
        }

        if("add" === handle){
            //添加
           if(ele_input_model[0].files.length > 0){
                let file = ele_input_model[0].files[0];
                let fs_name = file.name;
                let fs_size = parseInt(file.size);//文件字节大小
                let fs_size_m = parseInt(fs_size / 1024 / 1024); //换算成M单位
                if(!fs_name.endsWith(".tar")){
                    myAlert("模型文件格式必须是tar压缩包","error");
                    return false;
                }
                if(fs_size_m > 1000){
                    myAlert("模型文件不能超过1000M:"+String(fs_size_m),"error");
                    return false;
                }
           }else{
               myAlert("请上传模型文件","error");
               return false;
           }

        }
      }else if("API" === selectedWayCode){
            let way_value = ele_textarea_way_value.val().trim();
            if(way_value==="0" || way_value===""){
                myAlert("请输入接入参数","error");
                return false;
            }

      }else{
          myAlert("请选择接入方式","error");
          return false;
      }

   });
});

</script>

{% endblock javascripts %}
